
#include "stdafx.h"
#include "ConstDef.h"
#include "ReloadCode.h"
#include "ReloadPacket.h"
#include "ReloadConfigure.h"
#include "ReloadApp.h"
#include "ReloadPacket.h"
#include "ReloadOverlay.h"

namespace reload
{
	Kind::Kind()
	{

	}

	Kind::~Kind()
	{

	}

	////////////////////////////////////////////////////////////////////////////

	KindBlock::KindBlock()
	{

	}

	KindBlock::~KindBlock()
	{

	}

	///////////////////////////////////////////////////////////////////////////

	OverlayConfiguration::OverlayConfiguration()
	:sequence_(0),topologyPlugin_(OverlayAlgorithmType_ChordReload),nodeIdLength_(16),chordUpdateInterval_(600),chordPingInterval_(3600),chordReactive_(true),maxMessageSize_(5000),
	initialTtl_(100),requiredKinds_()
	{
		KindId kindId = KINDID_GENERIC_SINGLE;
		requiredKinds_[kindId].kind_.kindId_ = kindId;
		requiredKinds_[kindId].kind_.dataModel_ = DataModel_Single;
		requiredKinds_[kindId].kind_.maxCount_ = 1;
		requiredKinds_[kindId].kind_.maxSize_ = 0xffffffff;
		kindId = KINDID_GENERIC_ARRAY;
		requiredKinds_[kindId].kind_.kindId_ = kindId;
		requiredKinds_[kindId].kind_.dataModel_ = DataModel_Array;
		requiredKinds_[kindId].kind_.maxCount_ = 0xffffffff;
		requiredKinds_[kindId].kind_.maxSize_ = 0xffffffff;
		kindId = KINDID_GENERIC_DICTIONARY;
		requiredKinds_[kindId].kind_.kindId_ = kindId;
		requiredKinds_[kindId].kind_.dataModel_ = DataModel_Dictionary;
		requiredKinds_[kindId].kind_.maxCount_ = 0xffffffff;
		requiredKinds_[kindId].kind_.maxSize_ = 0xffffffff;
		initialTtl_ = 10;
	}

	OverlayConfiguration::~OverlayConfiguration()
	{
		requiredKinds_.clear();
	}

	//////////////////////////////////////////////////////////////////////////////////////////////

	ReloadConfigure::ReloadConfigure()
	:overlay_()
	{

	}

	ReloadConfigure::~ReloadConfigure()
	{
		overlay_.clear();
	}

	retT ReloadConfigure::init( const std::string _configFileName )
	{
		boost::shared_ptr<ReloadApp> pReloadApp = boost::dynamic_pointer_cast<ReloadApp>(asyframe::getApp());
		if(!pReloadApp)
			RET(ERROR_COMMON_SHARED_PTR_NULL);
		ReloadAppConfigure* pReloadAppConfigure = (ReloadAppConfigure*)pReloadApp->getConfigure();
		if(!pReloadAppConfigure)
			RET(ERROR_COMMON_PTR_NULL);
		for(boost::unordered_map<asyframe::uint32, ReloadAppOverlayConfigure>::iterator it = pReloadAppConfigure->overlay_.begin(); it != pReloadAppConfigure->overlay_.end(); ++it)
		{
			overlay_[it->first].instanceName_ = it->second.instanceName_;
		}
		RETSUCCESS;
	}

	///////////////////////////////////////////////////////////////////////////

	ReloadAppOverlayConfigure::ReloadAppOverlayConfigure()
	{

	}

	ReloadAppOverlayConfigure::~ReloadAppOverlayConfigure()
	{

	}

	///////////////////////////////////////////////////////////////////////////

	ReloadAppConfigure::ReloadAppConfigure()
	:reloadConnectionActorThreadSize_(0)
	{

	}

	ReloadAppConfigure::~ReloadAppConfigure()
	{
		reloadConnectionActorThreadSize_ = 0;
	}

	retT ReloadAppConfigure::init( const std::string _configFileName/*=DEFAULT_CONFIG_FILE_NAME*/ )
	{
		RNL(asyframe::ConfigureBase::init(RELOAD_CONFIG_FILE_NAME));
		try
		{
			reloadConfigureFileName_ = pt_.get<std::string>("Config.Reload.reloadConfigureFileName");
			reloadConnectionActorThreadSize_ = pt_.get<std::size_t>("Config.Reload.reloadConnectionActorThreadSize");
			
			std::size_t overlayNum = pt_.get<std::size_t>("Config.Reload.OverlayNum");
			for(std::size_t i = 0; i < overlayNum; ++i)
			{
				std::string configPrefix = "Config.Reload.Overlay";
				configPrefix += boost::lexical_cast<std::string>(i + 1);
				std::string instanceName = pt_.get<std::string>(configPrefix + ".instanceName");
				asyframe::uint32 overlay = SHA1HashValue(instanceName.c_str());
				overlay_[overlay].instanceName_ = instanceName;
				overlay_[overlay].listenIP_ = pt_.get<std::string>(configPrefix + ".Socket.listenIP");
				overlay_[overlay].listenPort_ = pt_.get<unsigned short>(configPrefix + ".Socket.listenPort");
				overlay_[overlay].nodeIdGen_ = pt_.get<std::string>(configPrefix + ".nodeIdGen");
				overlay_[overlay].isBootstrap_ = pt_.get<bool>(configPrefix + ".isBootstrap");
				std::size_t bootstrapNum = pt_.get<std::size_t>(configPrefix + ".BootstrapNum");
				overlay_[overlay].bootstrapAddrList_.resize(bootstrapNum);
				for(std::size_t j = 0; j < bootstrapNum; ++j)
				{
					std::string bootstrapPrefix = configPrefix + ".Bootstrap";
					bootstrapPrefix += boost::lexical_cast<std::string>(j + 1);
					overlay_[overlay].bootstrapAddrList_[j].bootstrapIP_ = pt_.get<std::string>(bootstrapPrefix + ".bootstrapIP");
					overlay_[overlay].bootstrapAddrList_[j].bootstrapPort_ = pt_.get<unsigned short>(bootstrapPrefix + ".bootstrapPort");
				}
				overlay_[overlay].requiredFingerTableSize_ = pt_.get<std::size_t>(configPrefix + ".requiredFingerTableSize");
				overlay_[overlay].maxNeighborTableSize_ = pt_.get<std::size_t>(configPrefix + ".maxNeighborTableSize");
				overlay_[overlay].maxFingerTableEntrySize_ = pt_.get<std::size_t>(configPrefix + ".maxFingerTableEntrySize");
				overlay_[overlay].nodeRole_ = pt_.get<NodeRoleT>(configPrefix + ".nodeRole");
			}
		}
		catch(std::exception *)
		{
			return ERROR_APPBASE_INIT_READ_CONFIG_FAIL;
		}
		return CONST_COMMON_SUCCESS;
	}
} /*namespace reload*/
